//***定义为全局变量***
//获取驱动信息
var driver = JSON.parse($("#dirverJson").text());

//更新Mysql配置
function updateConfigMysql($configContainer, mapping){
	var $username = $configContainer.find("input[name='username']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $table = $configContainer.find("input[name='table']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	var $url = $configContainer.find("textarea[name='url']").val();
	
	mapping.config.username = $username;
	mapping.config.password = $password;
	mapping.config.table = $table;
	mapping.config.url = $url;
	mapping.config.primaryKey = $primaryKey;
	mapping.name = $table;
}

//更新Oracle配置
function updateConfigOracle($configContainer, mapping){
	var $username = $configContainer.find("input[name='username']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $table = $configContainer.find("input[name='table']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	var $url = $configContainer.find("textarea[name='url']").val();
	
	mapping.config.username = $username;
	mapping.config.password = $password;
	mapping.config.table = $table;
	mapping.config.url = $url;
	mapping.config.primaryKey = $primaryKey;
	mapping.name = $table;
}

//更新SqlServer配置
function updateConfigSqlServer($configContainer, mapping){
	var $username = $configContainer.find("input[name='username']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $table = $configContainer.find("input[name='table']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	var $url = $configContainer.find("textarea[name='url']").val();
	
	mapping.config.username = $username;
	mapping.config.password = $password;
	mapping.config.table = $table;
	mapping.config.primaryKey = $primaryKey;
	mapping.config.url = $url;
	mapping.name = $table;
}

//更新SqlMysql配置
function updateConfigSqlMysql($configContainer, mapping){
	var $username = $configContainer.find("input[name='username']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $name = $configContainer.find("input[name='name']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	var $sql = $configContainer.find("textarea[name='sql']").val();
	var $url = $configContainer.find("textarea[name='url']").val();
	
	mapping.config.username = $username;
	mapping.config.password = $password;
	mapping.name = $name;
	mapping.config.primaryKey = $primaryKey;
	mapping.config.sql = $sql;
	mapping.config.url = $url;
}

//更新SqlOracle配置
function updateConfigSqlMysql($configContainer, mapping){
	var $username = $configContainer.find("input[name='username']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $name = $configContainer.find("input[name='name']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	var $sql = $configContainer.find("textarea[name='sql']").val();
	var $url = $configContainer.find("textarea[name='url']").val();
	
	mapping.config.username = $username;
	mapping.config.password = $password;
	mapping.name = $name;
	mapping.config.primaryKey = $primaryKey;
	mapping.config.sql = $sql;
	mapping.config.url = $url;
}

//更新WebService配置
function updateConfigWebService($configContainer, mapping){
	var $url = $configContainer.find("input[name='url']").val();
	var $method = $configContainer.find("input[name='method']").val();
	var $name = $configContainer.find("input[name='name']").val();
	var $primaryKey = $configContainer.find("input[name='primaryKey']").val();
	
	mapping.name = $name;
	mapping.config.url = $url;
	mapping.config.method = $method;
	mapping.config.primaryKey = $primaryKey;
}

//更新Ldap配置
function updateConfigLdap($configContainer, mapping){
	var $name = $configContainer.find("input[name='name']").val();
	var $url = $configContainer.find("input[name='url']").val();
	var $base = $configContainer.find("input[name='base']").val();
	var $userDn = $configContainer.find("input[name='userDn']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $dn = $configContainer.find("input[name='dn']").val();

	mapping.name = $name;
	mapping.config.url = $url;
	mapping.config.base = $base;
	mapping.config.userDn = $userDn;
	mapping.config.password = $password;
	mapping.config.dn = $dn;
}

//更新Redis配置
function updateConfigRedis($configContainer, mapping){
	var $name = $configContainer.find("input[name='name']").val();
	var $password = $configContainer.find("input[name='password']").val();
	var $key = $configContainer.find("input[name='key']").val();
	var $url = $configContainer.find("textarea[name='url']").val();

	mapping.name = $name;
	mapping.config.password = $password;
	mapping.config.key = $key;
	mapping.config.url = $url;
}

// 按类型更新配置
function updateConfigType($configContainer, mapping){
	// 获取连接类型
	var conn = $configContainer.find("input[name='connector']").attr("conn");
	switch (conn) {
	case ConnectorConstantMysql:
		updateConfigMysql($configContainer, mapping);
		break;
	case ConnectorConstantOracle:
		updateConfigOracle($configContainer, mapping);
		break;
	case ConnectorConstantSqlServer:
		updateConfigSqlServer($configContainer, mapping);
		break;
	case ConnectorConstantSqlMysql:
		updateConfigSqlMysql($configContainer, mapping);
		break;
	case ConnectorConstantSqlOracle:
		updateConfigSqlMysql($configContainer, mapping);
		break;
	case ConnectorConstantSourceWebService:
		updateConfigWebService($configContainer, mapping);
		break;
	case ConnectorConstantTargetWebService:
		updateConfigWebService($configContainer, mapping);
		break;
	case ConnectorConstantLdap:
		updateConfigLdap($configContainer, mapping);
		break;
	case ConnectorConstantRedis:
		updateConfigRedis($configContainer, mapping);
		break;
	default:
		break;
	}
}

// 保存驱动配置
function saveDriverConfig(driver) {
	// 校验表单
	if($(".connectorConfig").formValidate()==false){
		return null;
	}
	
	// 更新配置
	updateConfigType($("#sourceConfig"), driver.sourceMapping);
	updateConfigType($("#targetConfig"), driver.targetMapping);
	
	// 保存驱动配置
	$.loadingT(true);
	$.post($basePath + "/driver/update", {
		"mapping" : JSON.stringify(driver)
	}, function(data) {
		$.loadingT(false);
		if (data.success == true) {
			bootGrowl(data.resultValue, "success");
			$initContainer.load($basePath + "/index");
		} else {
			bootGrowl(data.resultValue, "danger");
		}
	});
}

//Mysql
function driverMysql($configContainer, config){
	var $username = $configContainer.find("input[name='username']");
	var $password = $configContainer.find("input[name='password']");
	var $table = $configContainer.find("input[name='table']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$username.val(config.username);
	$password.val(config.password);
	$table.val(config.table);
	$primaryKey.val(config.primaryKey);
	$url.val(config.url);
}

//Oracle
function driverOracle($configContainer, config){
	var $username = $configContainer.find("input[name='username']");
	var $password = $configContainer.find("input[name='password']");
	var $table = $configContainer.find("input[name='table']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$username.val(config.username);
	$password.val(config.password);
	$table.val(config.table);
	$primaryKey.val(config.primaryKey);
	$url.val(config.url);
}

//SqlServer
function driverSqlServer($configContainer, config){
	var $username = $configContainer.find("input[name='username']");
	var $password = $configContainer.find("input[name='password']");
	var $name = $configContainer.find("input[name='name']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	var $driver = $configContainer.find("input[name='driver']");
	var $sql = $configContainer.find("textarea[name='sql']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$username.val(config.username);
	$password.val(config.password);
	$name.val(name);
	$primaryKey.val(config.primaryKey);
	// 将下拉替换为input元素
	$driver.val(config.driverClassName);
	$sql.val(config.sql);
	$url.val(config.url);
}

// SqlMysql
function driverSqlMysql($configContainer, config, name){
	var $username = $configContainer.find("input[name='username']");
	var $password = $configContainer.find("input[name='password']");
	var $name = $configContainer.find("input[name='name']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	var $driver = $configContainer.find("input[name='driver']");
	var $sql = $configContainer.find("textarea[name='sql']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$username.val(config.username);
	$password.val(config.password);
	$name.val(name);
	$primaryKey.val(config.primaryKey);
	// 将下拉替换为input元素
	$driver.val(config.driverClassName);
	$sql.val(config.sql);
	$url.val(config.url);
}

//SqlMysql
function driverSqlOracle($configContainer, config, name){
	var $username = $configContainer.find("input[name='username']");
	var $password = $configContainer.find("input[name='password']");
	var $name = $configContainer.find("input[name='name']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	var $driver = $configContainer.find("input[name='driver']");
	var $sql = $configContainer.find("textarea[name='sql']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$username.val(config.username);
	$password.val(config.password);
	$name.val(name);
	$primaryKey.val(config.primaryKey);
	// 将下拉替换为input元素
	$driver.val(config.driverClassName);
	$sql.val(config.sql);
	$url.val(config.url);
}

// Webservice
function driverWebservice($configContainer, mapping){
	var $url = $configContainer.find("input[name='url']");
	var $method = $configContainer.find("input[name='method']");
	var $name = $configContainer.find("input[name='name']");
	var $primaryKey = $configContainer.find("input[name='primaryKey']");
	
	$url.val(mapping.config.url);
	$method.val(mapping.config.method);
	$name.val(mapping.name);
	$primaryKey.val(mapping.config.primaryKey);
	
	// 隐藏字段配置
	$configContainer.find(".driverWebServiceParamsContainer").remove();
}

// Ldap
function driverLdap($configContainer, mapping){
	var $name = $configContainer.find("input[name='name']");
	var $url = $configContainer.find("input[name='url']");
	var $base = $configContainer.find("input[name='base']");
	var $userDn = $configContainer.find("input[name='userDn']");
	var $password = $configContainer.find("input[name='password']");
	var $dn = $configContainer.find("input[name='dn']");
	
	$name.val(mapping.name);
	$url.val(mapping.config.url);
	$base.val(mapping.config.base);
	$userDn.val(mapping.config.userDn);
	$password.val(mapping.config.password);
	$dn.val(mapping.config.dn);
	
	// 隐藏字段配置
	$configContainer.find(".driverEditHideItem").remove();
}

//Redis
function driverRedis($configContainer, mapping){
	var $name = $configContainer.find("input[name='name']");
	var $password = $configContainer.find("input[name='password']");
	var $key = $configContainer.find("input[name='key']");
	var $url = $configContainer.find("textarea[name='url']");
	
	$name.val(mapping.name);
	$password.val(mapping.config.password);
	$key.val(mapping.config.key);
	$url.val(mapping.config.url);
	// 隐藏字段配置
	$configContainer.find(".driverEditHideItem").remove();
}

// 加载驱动配置
function loadDriverConfig($configContainer, mapping){
	switch (mapping.connector) {
	case ConnectorConstantMysql:
		driverMysql($configContainer, mapping.config);
		break;
	case ConnectorConstantOracle:
		driverOracle($configContainer, mapping.config);
		break;
	case ConnectorConstantSqlServer:
		driverSqlServer($configContainer, mapping.config);
		break;
	case ConnectorConstantSqlMysql:
		driverSqlMysql($configContainer, mapping.config, mapping.name);
		break;
	case ConnectorConstantSqlOracle:
		driverSqlOracle($configContainer, mapping.config, mapping.name);
		break;
	case ConnectorConstantWebService:
		driverWebservice($configContainer, mapping);
		break;
	case ConnectorConstantLdap:
		driverLdap($configContainer, mapping);
		break;
	case ConnectorConstantRedis:
		driverRedis($configContainer, mapping);
		break;
	default:
		break;
	}
}

// 加载连接器页面
function loadDriverConn($container, mapping, flag){
	// 显示配置的页面区域
	var $configContainer = $container.find(".connectorConfig");
	$configContainer.html("");
	// 连接器类型
	var $connType = $container.parent().find("input[name='connector']");
	
	var configUrl = '';
	// Webservice 特殊处理
	if(ConnectorConstantWebService==mapping.connector){
		// 数据源or目标源
		var conn = "source"==flag ?　ConnectorConstantSourceWebService : ConnectorConstantTargetWebService;
		configUrl = DriverConnectorConstant[conn]['url'];
	}else{
		configUrl = DriverConnectorConstant[mapping.connector]['url'];
	}
	// 获取连接器名称
	$connType.val(DriverConnectorConstant[mapping.connector]['name']);
	$connType.attr("conn",mapping.connector);
	
	// 加载驱动配置
	$configContainer.load($basePath+configUrl, function(rep,status,xhr){
		if('success'==status){
			loadDriverConfig($configContainer, mapping);
		}
	});
}

// 加载目标源和数据源配置
function loadDriver(){
	//连接器类型
	var sourceMapping = driver.sourceMapping;
	var targetMapping = driver.targetMapping;
	
	// 数据源配置
	loadDriverConn($("#sourceConfig"), sourceMapping, "source");
	loadDriverConn($("#targetConfig"), targetMapping, "target");
}

$(function() {
	// 加载目标源和数据源配置
	loadDriver();
	
	// 保存
	$("#driverSubBtn").click(function() {
		saveDriverConfig(driver);
	});
	
	//返回
	$("#driverBackIndexBtn").click(function(){
		//加载页面,在IE浏览器下,防止请求从缓存加载,故加上refresh时间戳参数
		$initContainer.load($basePath+"/index?refresh="+new Date().getTime());
	});
})